package com.hskn.hss.module.casesetissue.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.casesetissue.entity.CasesetIssue;
import com.hskn.hss.module.casesetissue.mapper.CasesetIssueMapper;
import com.hskn.hss.module.casesetissue.service.ICasesetIssueService;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-18
 */
@Service
public class CasesetIssueServiceImpl extends ServiceImpl<CasesetIssueMapper, CasesetIssue> implements ICasesetIssueService {

    @Resource
    private CasesetIssueMapper casesetIssueMapper;

    @Override
    public List<TreeEntity> tree(JSONObject params) throws HssException {

        List<TreeEntity> casesetIssues = casesetIssueMapper.getTreeList();
        List<TreeEntity> treeEntities = TreeUtil.RecursiveAddress(casesetIssues);
        return treeEntities;
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("casesetIssueId"))) {
            throw new HssException("参数错误");
        }
        CasesetIssue casesetIssue = JSON.parseObject(params.toJSONString(), CasesetIssue.class);
        casesetIssueMapper.updateById(casesetIssue);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        CasesetIssue casesetIssue = JSON.parseObject(params.toJSONString(), CasesetIssue.class);
        casesetIssueMapper.insert(casesetIssue);
    }

    @Override
    public void del(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("casesetIssueId"))) {
            throw new HssException("参数错误");
        }
        String id = params.getString("casesetIssueId");
        casesetIssueMapper.deleteById(id);
    }
}
